package com.example.opengldemo.jbulletJava;

import android.view.View;

import javax.vecmath.Quat4f;
import javax.vecmath.Vector3f;

import com.bulletphysics.collision.shapes.CollisionShape;
import com.bulletphysics.dynamics.DiscreteDynamicsWorld;
import com.bulletphysics.dynamics.RigidBody;
import com.bulletphysics.dynamics.RigidBodyConstructionInfo;
import com.bulletphysics.linearmath.DefaultMotionState;
import com.bulletphysics.linearmath.Transform;
import com.example.opengldemo.util.MatrixState;

import static com.example.opengldemo.jbulletJava.Constant.isNumber;

public class TexCube extends BNThing{
    TextureRect tr;
    float halfSize;
    RigidBody body;
    View mv;
    int mProgram ;

    public TexCube(View mv,
                   float halfSize,
                   CollisionShape colShape,
                   DiscreteDynamicsWorld dynamicsWorld,
                   float mass,
                   float cx,
                   float cy,
                   float cz,
                   int mProgram) {
        boolean isDynamic = (mass != 0f);
        Vector3f localInertia = new Vector3f(0, 0, 0);
        if (isDynamic) {
            colShape.calculateLocalInertia(mass, localInertia);
        }
        Transform startTransform = new Transform();
        startTransform.setIdentity();
        startTransform.origin.set(new Vector3f(cx, cy, cz));

        DefaultMotionState myMotionState = new DefaultMotionState(startTransform);

        RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo
                (mass, myMotionState, colShape, localInertia);
        body = new RigidBody(rbInfo);
        body.setRestitution(0.6f);
        body.setFriction(0.8f);
        dynamicsWorld.addRigidBody(body);
        this.mv = mv;
        tr = new TextureRect(halfSize);
        this.halfSize = halfSize;
        this.mProgram = mProgram ;

    }

    public TexCube(View mv,
                   float halfSize,
                   RigidBody body ,
                   int mProgram) {
        this.body = body ;
        this.mv = mv;
        this.halfSize = halfSize;
        this.mProgram = mProgram ;
        tr = new TextureRect(halfSize);

    }

    public TexCube(float halfSize, int mProgram) {
        tr = new TextureRect(halfSize);
        this.halfSize = halfSize;
        this.mProgram = mProgram ;

    }

    public void drawSelf(int[] texIda) {
        tr.initShader(mProgram);
        int texId = texIda[0];
        MatrixState.pushMatrix();
        MySurfaceView3.init = false;

        if (body != null) {
            if (!body.isActive()) {
                texId = texIda[1];
            }
            Transform trans = body.getMotionState().getWorldTransform(new Transform());
            MatrixState.translate(trans.origin.x, trans.origin.y, trans.origin.z);
            Quat4f ro = trans.getRotation(new Quat4f());
            if (ro.x != 0 || ro.y != 0 || ro.z != 0) {
                float[] fa = SYSUtil.fromSYStoAXYZ(ro);
                //MatrixState.rotate(fa[0], fa[1], fa[2], fa[3]);
                if (isNumber(fa[0] + "") && isNumber(fa[1] + "") && isNumber(fa[2] + "")) {
                    MatrixState.rotate(fa[0], fa[1], fa[2], fa[3]);
                }
            }
        }



        MatrixState.pushMatrix();
        MatrixState.translate(0, halfSize, 0);
        MatrixState.rotate(-90, 1, 0, 0);
        tr.drawSelf(texId);
        MatrixState.popMatrix();

        MatrixState.pushMatrix();
        MatrixState.translate(0, -halfSize, 0);
        MatrixState.rotate(90, 1, 0, 0);
        tr.drawSelf(texId);
        MatrixState.popMatrix();

        MatrixState.pushMatrix();
        MatrixState.translate(-halfSize, 0, 0);
        MatrixState.rotate(-90, 0, 1, 0);
        tr.drawSelf(texId);
        MatrixState.popMatrix();

        MatrixState.pushMatrix();
        MatrixState.translate(halfSize, 0, 0);
        MatrixState.rotate(90, 0, 1, 0);
        tr.drawSelf(texId);
        MatrixState.popMatrix();

        MatrixState.pushMatrix();
        MatrixState.translate(0, 0, halfSize);
        tr.drawSelf(texId);
        MatrixState.popMatrix();

        MatrixState.pushMatrix();
        MatrixState.translate(0, 0, -halfSize);
        MatrixState.rotate(180, 0, 1, 0);
        tr.drawSelf(texId);
        MatrixState.popMatrix();

        MatrixState.popMatrix();
    }

}
